package org.example.rabbitmq.workWithFairDispatch;

import com.rabbitmq.client.*;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 1: 创建连接工厂，设置连接属性
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

        // 2: 创建连接，获取通道
        Connection connection = connectionFactory.newConnection("生产者");
        Channel channel = connection.createChannel();

        // 3: 声明队列
        channel.queueDeclare("work-queue2", false, false, false, null);
        for (int i = 1; i <= 20; i++) {
            channel.basicPublish("", "work-queue2", null, ("work-公平分发:" + i).getBytes());
        }
        System.out.println("消息发送成功!");

        // 最后关闭通关和连接
        channel.close();
        connection.close();
    }
}
